package com.fc.tms.service.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.fc.tms.constant.UserType;
import com.fc.tms.dao.UserDAO;
import com.fc.tms.entity.User;
import com.fc.tms.filter.UserFilter;
import com.fc.tms.service.UserService;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource
    UserDAO userDAO;

    @Override
    public List<User> find(UserFilter filter) {
        return userDAO.find(filter);
    }

    @Override
    @Transactional
    public void saveOrUpdate(User user) {
        userDAO.saveOrUpdate(user);
    }

    @Override
    public User get(Long tId) {
        return userDAO.get(tId);
    }

    @Override
    @Transactional
    public void delete(Long tId) {
        userDAO.remove(tId);
    }

    @Override
    public boolean isExist(String eName) {
        return userDAO.isExist(eName);
    }

    @Override
    public User getByeName(String eName) {
        return userDAO.getByeName(eName);
    }

    @Override
    public Long insertNotExists(String eName, UserType userType) {
        User user = new User();
        user.setUserType(userType);
        user.seteName(eName);

        Long studentId = null;
        User dbUser = this.getByeName(user.geteName());
        if (dbUser == null) {
            this.saveOrUpdate(user);

            studentId = user.getId();
        } else {
            studentId = dbUser.getId();
        }

        return studentId;
    }

    @Override
    public Integer dimission(String eName) {
        if (StringUtils.isNotBlank(eName)) {
            return userDAO.dimission(eName);
        }
        
        return 0;
    }
}